package com.fzrskj.cloud.free.sys.utils;

import java.util.Map;
import java.util.List;

import com.fzrskj.cloud.free.sys.dao.SysProductDiycatDao;
import com.fzrskj.cloud.free.sys.bean.SysProductDiycatBean;
import ms.core.object.CObject;
import ms.core.consts.SysErr;
import org.apache.commons.collections.map.HashedMap;

public class SysProductDiycatUtils extends CObject{
	//private declarations
	private SysProductDiycatDao dao=null;

	//constructor function to initialize database access object
	public SysProductDiycatUtils(){ dao=new SysProductDiycatDao(); }

	//to free resource
	@Override
	public void dispose(){
		if (dao!=null) {
			dao.dispose();
		}
		dao = null;
		super.dispose();
	}

	//new record by map
	public int newSysProductDiycat(Map<String, Object> values){
		int ret = dao.newSysProductDiycat(values);
		this.setErrCode(dao.getErrCode());
		return ret;
	}

	//delete record by id
	public int delSysProductDiycat(int id){
		int ret = dao.delSysProductDiycat(id);
		this.setErrCode(dao.getErrCode());
		if (ret==0) {
			this.setErrCode(SysErr.SYS_DATA_NOT_FOUND);
		}
		return ret;
	}

	//modify record by map
	public int modSysProductDiycat(Map<String,Object> values, Map<String,Object> cdt){
		int ret = dao.modSysProductDiycat(values, cdt);
		this.setErrCode(dao.getErrCode());
		if (ret==0) {
			this.setErrCode(SysErr.SYS_DATA_NOT_FOUND);
		}
		return ret;
	}

	//query record by id
	public SysProductDiycatBean querySysProductDiycat(int id){
	    SysProductDiycatBean ret = dao.querySysProductDiycat(id);
		String err = dao.getErrCode();
		this.setErrCode(err);
		if (err.equals(SysErr.CODE_SUCCESS) && ret==null) {
			this.setErrCode(SysErr.SYS_DATA_NOT_FOUND);
		}
		return ret;
	}

	/**
	 * 通过条件获取一个实例.
	 * @param cdt cdt.
	 * @return SysProductDiycatBean.
	 */
	public SysProductDiycatBean getSysProductDiycat(Map<String, Object> cdt) {
		List<SysProductDiycatBean> rets = this.dao.querySysProductDiycat(cdt, null, 1, 1);
		this.setErrCode(dao.getErrCode());
		return rets.isEmpty() ? null : rets.get(0);
	}

	//query record count by cdt
	public int querySysProductDiycat(Map<String,Object> cdt){
		int ret = dao.querySysProductDiycat(cdt);
		this.setErrCode(dao.getErrCode());
		return ret;
	}

	//query record list by cdt
	public List<SysProductDiycatBean> querySysProductDiycat(Map<String,Object> cdt,String orderby, int page,int pagesize){
		List<SysProductDiycatBean> rets = dao.querySysProductDiycat(cdt,orderby,page,pagesize);
		String err = dao.getErrCode();
		this.setErrCode(err);
		if (err.equals(SysErr.CODE_SUCCESS) && (rets==null || rets.size()==0)){
			this.setErrCode(SysErr.SYS_DATA_NOT_FOUND);
		}
		return rets;
	}

	/**
	 * 获取所有可用列表.
	 * @return
	 */
	public List<SysProductDiycatBean> queryUsableList() {
		Map<String, Object> cdt = new HashedMap();
		cdt.put("status", 1);
		List<SysProductDiycatBean> rets = this.querySysProductDiycat(cdt, "sortNum", 1, -1);
		return rets;
	}

	/**
	 * 获取所有列表.
	 * @return
	 */
	public List<SysProductDiycatBean>  queryAllList() {
		List<SysProductDiycatBean> rets = this.querySysProductDiycat(new HashedMap(), "sortNum", 1, -1);
		return rets;
	}
}